/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.ClieUserStatus;
import com.minierp.bean.LoginBean;
import com.minierp.dao.ClieUserDao;
import com.minierp.dao.EnterpriseInfoDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncLoginFormCtrHelper;
import com.minierp.model.ClieUser;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.Establishment;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class ClieUserLoginFormCtr extends SimpleFormController {
	private ClieUserDao clieUserDao;
	private EnterpriseInfoDao enterpriseInfoDao;
	private DataSource dataSource;

	public ClieUserDao getClieUserDao() {
		return this.clieUserDao;
	}

	public void setClieUserDao(ClieUserDao clieUserDao) {
		this.clieUserDao = clieUserDao;
	}

	public EnterpriseInfoDao getEnterpriseInfoDao() {
		return this.enterpriseInfoDao;
	}

	public void setEnterpriseInfoDao(EnterpriseInfoDao enterpriseInfoDao) {
		this.enterpriseInfoDao = enterpriseInfoDao;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		EnterpriseInfo en = this.enterpriseInfoDao.get();
		EncCommonHelper.setEnterpriseInfo(request, en);
		EncLoginFormCtrHelper.parseLicenseInfo(request,
				this.getServletContext(), this.dataSource);
		return new LoginBean();
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		LoginBean bean = (LoginBean) command;
		bean.setRandomCode(EncCommonHelper.getCurrentRandomImageCode(request));
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		LoginBean bean = (LoginBean) command;
		ClieUser u = this.clieUserDao.get(bean.getUserName());
		Establishment est = null;
		if (u != null) {
			est = (Establishment) this.clieUserDao.get(Establishment.class,
					u.getClieId());
		}

		ClieUserStatus s = EncLoginFormCtrHelper.checkClieUser(u,
				bean.getPassword(), est, request);
		String alertMsg = s.getAlertMsg();
		if (alertMsg.equals("")) {
			String map1 = request.getParameter("signonForwardUrl");
			if (map1 != null && !map1.equals("")) {
				response.sendRedirect(map1);
				return null;
			} else {
				ModelAndView mv = new ModelAndView(this.getSuccessView());
				return mv;
			}
		} else {
			HashMap map = new HashMap();
			map.put("command", bean);
			EncCommonHelper.setAlertMessage(request, alertMsg);
			return new ModelAndView(this.getFormView(), map);
		}
	}
}